package org.ccar.ar;

import android.graphics.Canvas;
import android.graphics.Color;

public class Radar {
	public static final float RADIUS = 60;

	private static final int LINE_COLOR = Color.argb(100, 255, 255, 255);
	private static final float PAD_X = 10;
	private static final float PAD_Y = 90;
	private static final int RING_COLOR = Color.argb(200, 255, 255, 255);
	private static final int RADAR_COLOR = Color.argb(100, 100, 100, 100);
	private static final int TEXT_COLOR = Color.rgb(255, 255, 255);
	private static final int TEXT_SIZE = 12;

	private static ScreenPositionUtility leftRadarLine = null;
	private static ScreenPositionUtility rightRadarLine = null;
	private static PaintablePosition leftLineContainer = null;
	private static PaintablePosition rightLineContainer = null;
	private static PaintablePosition circleContainer = null;
	private static PaintablePosition ringContainer = null;

	private static PaintableRadarPoints radarPoints = null;
	private static PaintablePosition pointsContainer = null;

	private static PaintableText paintableText = null;
	private static PaintablePosition paintedContainer = null;

	public Radar() {
		if (leftRadarLine == null)
			leftRadarLine = new ScreenPositionUtility();
		if (rightRadarLine == null)
			rightRadarLine = new ScreenPositionUtility();
	}

	public void draw(Canvas canvas) {
		if (canvas == null)
			throw new NullPointerException();

		PitchAzimuthCalculator.calcPitchBearing(ARData.getRotationMatrix());
		ARData.setAzimuth(PitchAzimuthCalculator.getAzimuth());
		ARData.setPitch(PitchAzimuthCalculator.getPitch());

		drawRadarCircle(canvas);
		drawRadarPoints(canvas);
		drawRadarLines(canvas);
		drawRadarText(canvas);
	}

	private void drawRadarCircle(Canvas canvas) {
		if (canvas == null)
			throw new NullPointerException();

		if (ringContainer == null) {
			PaintableCircle paintableRing = new PaintableCircle(RING_COLOR,
					RADIUS, false);
			paintableRing.setStrokeWidth(3);
			ringContainer = new PaintablePosition(paintableRing, PAD_X
					+ RADIUS, PAD_Y + RADIUS, 0, 1);
		}
		ringContainer.paint(canvas);
		
		if (circleContainer == null) {
			PaintableCircle paintableCircle = new PaintableCircle(RADAR_COLOR,
					RADIUS, true);
			circleContainer = new PaintablePosition(paintableCircle, PAD_X
					+ RADIUS, PAD_Y + RADIUS, 0, 1);
		}
		circleContainer.paint(canvas);
	}

	private void drawRadarPoints(Canvas canvas) {
		if (canvas == null)
			throw new NullPointerException();

		if (radarPoints == null)
			radarPoints = new PaintableRadarPoints();

		if (pointsContainer == null)
			pointsContainer = new PaintablePosition(radarPoints, PAD_X, PAD_Y,
					-ARData.getAzimuth(), 1);
		else
			pointsContainer.set(radarPoints, PAD_X, PAD_Y,
					-ARData.getAzimuth(), 1);

		pointsContainer.paint(canvas);
	}

	private void drawRadarLines(Canvas canvas) {
		if (canvas == null)
			throw new NullPointerException();

		if (leftLineContainer == null) {
			leftRadarLine.set(0, -RADIUS + 10);
			leftRadarLine.rotate(-CameraModel.DEFAULT_VIEW_ANGLE / 2);
			leftRadarLine.add(PAD_X + RADIUS, PAD_Y + RADIUS);

			float leftX = leftRadarLine.getX() - (PAD_X + RADIUS);
			float leftY = leftRadarLine.getY() - (PAD_Y + RADIUS);
			PaintableLine leftLine = new PaintableLine(LINE_COLOR, leftX, leftY);
			leftLine.setStrokeWidth(2);
			leftLineContainer = new PaintablePosition(leftLine, PAD_X + RADIUS,
					PAD_Y + RADIUS, 0, 1);
		}
		leftLineContainer.paint(canvas);

		if (rightLineContainer == null) {
			rightRadarLine.set(0, -RADIUS + 10);
			rightRadarLine.rotate(CameraModel.DEFAULT_VIEW_ANGLE / 2);
			rightRadarLine.add(PAD_X + RADIUS, PAD_Y + RADIUS);

			float rightX = rightRadarLine.getX() - (PAD_X + RADIUS);
			float rightY = rightRadarLine.getY() - (PAD_Y + RADIUS);
			PaintableLine rightLine = new PaintableLine(LINE_COLOR, rightX,
					rightY);
			rightLine.setStrokeWidth(2);
			rightLineContainer = new PaintablePosition(rightLine, PAD_X
					+ RADIUS, PAD_Y + RADIUS, 0, 1);
		}
		rightLineContainer.paint(canvas);
	}

	private void drawRadarText(Canvas canvas) {
		if (canvas == null)
			throw new NullPointerException();
		int range = (int) (ARData.getAzimuth() / (360f / 16f));
		String dirTxt = "";
		if (range == 15 || range == 0)
			dirTxt = "N";
		else if (range == 1 || range == 2)
			dirTxt = "NE";
		else if (range == 3 || range == 4)
			dirTxt = "E";
		else if (range == 5 || range == 6)
			dirTxt = "SE";
		else if (range == 7 || range == 8)
			dirTxt = "S";
		else if (range == 9 || range == 10)
			dirTxt = "SW";
		else if (range == 11 || range == 12)
			dirTxt = "W";
		else if (range == 13 || range == 14)
			dirTxt = "NW";
		int bearing = (int) ARData.getAzimuth();
		radarText(canvas, "" + bearing + ((char) 176) + " " + dirTxt,
				(PAD_X + RADIUS), (PAD_Y - 5), true);

//		radarText(canvas, GeoCalcUtil.formatDistance(ARData.getRadius()),
//				(PAD_X + RADIUS), (PAD_Y + RADIUS * 2 - 10), false);
	}

	private void radarText(Canvas canvas, String txt, float x, float y,
			boolean bg) {
		if (canvas == null || txt == null)
			throw new NullPointerException();

		if (paintableText == null)
			paintableText = new PaintableText(txt, TEXT_COLOR, TEXT_SIZE, bg);
		else
			paintableText.set(txt, TEXT_COLOR, TEXT_SIZE, bg);

		if (paintedContainer == null)
			paintedContainer = new PaintablePosition(paintableText, x, y, 0, 1);
		else
			paintedContainer.set(paintableText, x, y, 0, 1);

		paintedContainer.paint(canvas);
	}
}